home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / DNA / DNAV1I2.sit / DNAV1I2 / C-SER-1.TXT < prev    next >
Text File  |  1993-05-26  |  11KB  |  232 lines

  1.  
  2.                   …ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
  3.                   ∫ C Programming Series: Issue 1       ∫
  4.                   ∫ Released With DNA Volume 1, Issue 2 ∫
  5.                   ∫ Written by Pazuzu 04-21-1993        ∫
  6.                   »ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº
  7.  
  8.  
  9. Welcome to Issue #1 of Pazuzu's Guide to C Programming: The Fun, Easy, And 
  10. Possibly DESTRUCTIVE Way to Learn C!  
  11.  
  12. You may wonder why I'm writing an article on a non-underground topic. In 
  13. truth, it's really not any of your fucking business and you should just BUTT 
  14. OUT, MIND YOUR OWN FUCKING BUSINESS, AND ENJOY THE INFO. But, since I'm in a 
  15. good mood, I'll tell you anyway.
  16.  
  17. I've been seeing posts all over the place, on nets, local BBS's, everywhere, 
  18. with people saying "hOw Do YoU pRoGrAm In C?!^%$!$!$!!!?????". So, I decided 
  19. to write a tutorial series so that even the stupidest fool who can't even use 
  20. WINDOWS will be able to program in C, without damaging his hard drive (at 
  21. least not physically anyway.).  Actually, you can't be a total idiot, you DO 
  22. have to get a C compiler, and figure out how to use it, because I'm not 
  23. telling you how to use the compiler, only how to write programs. If you can't 
  24. accomplish the simple task of getting a C compiler and figuring out how to 
  25. use it (I recommend Turbo or Borland C, as some of my examples are 
  26. TC/BC-specific), then please put your computer in a suitable shipping box and 
  27. ship it to the PO box listed at the end of this article, then place a loaded 
  28. gun to your head and pull the trigger. 
  29.  
  30.  
  31. C programming is not particularly difficult, and I assume only a knowledge 
  32. of either BASIC or Pascal in this series. C will save you much time when 
  33. coding, just in typing alone. For example, in Pascal, to define a code block, 
  34. you have to type "Begin" and "End". This is utterly ridiculous. In C, it is 
  35. simply "{" and "}". Besides, Pascal was invented as a teaching language 
  36. anyway, while C was made BY programmers FOR programmers, and we ALL know what 
  37. BASIC is for, so WHY would you want to use anything but C?
  38.  
  39.  
  40.  
  41. Variables, Declarations, Type Specifiers, Etc
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. In BASIC, when you need a variable, you just use it. However, in most other 
  44. languages, you must DECLARE each variable before it is used. You do this by 
  45. telling the compiler the name of the variable and what type of data it needs 
  46. to hold. Here are a few examples in C:
  47.  
  48. int i;
  49. register int j;
  50. char a_string[9];
  51. unsigned char q;
  52.  
  53. The first line declares an integer variable named "i", which can hold any 
  54. value between -32,767 and +32,767 - the maximum values allowed in 16 bits. 
  55.  
  56. The second line is a special case - it tells the compiler to write code that
  57. will use a REGISTER for the variable, if one is available. You would do this 
  58. for any variables that are used for loop control, as the CPU can access a 
  59. register MUCH faster than a memory location, and any loop control variable is 
  60. going to be accessed many times. If there is no register available, the 
  61. compiler just makes it a normal variable - you don't need to worry about it. 
  62.  
  63. Line 3 is a very important example. There isn't a string data type in C - you
  64. must use arrays of char's. Line 3 defines an 8-character string. Why only 8?, 
  65. you ask... Well, in C, every string must end in a NULL (ASCII 0), and that 
  66. NULL takes up a position in memory, so you need to always make the strings 
  67. one bigger than what you really need. The NULL is handled automagically by 
  68. all of the library's string functions. 
  69.  
  70. Line 4 defines an 8-bit variable which can hold either an actual ASCII 
  71. character or an integer value in the range 0 to 255 - the maximum for 8 
  72. bits. If you define an unsigned int, you can hold a value between 0 and 
  73. 65,535. The reason for this is that C assumes you want a signed unless you 
  74. say unsigned. The highest bit is always used for the sign, so with a signed 
  75. int, you can have -32,767 to +32,767 (-128 thru +128 for signed char), and 
  76. with an unsigned, you get 0 thru 65,535 for int, and 0 thru 255 for char. 
  77. There's a few other type specifiers also, the most important being long. If 
  78. you apply long to an int, you get 32 bits, which is a pretty huge range, even 
  79. if it's signed. Unsigned, it's really huge. I don't remember the range off 
  80. the top of my head, and I don't want to load the compiler and check in the 
  81. help right now, so you'll just have to figure it out yourself. But trust me, 
  82. it's big enough to handle any values you could ever possibly want. 
  83.  
  84.  
  85.  
  86.  
  87. Very Basic C Programming
  88. ~~~~~~~~~~~~~~~~~~~~~~~~
  89.  
  90. With that out of the way, on to the structure of a C program... 
  91.  
  92. C is a weird language in that it defines NO, that's right - NO input/output 
  93. commands... "Well then how the fuck do I do input/output???" you ask. You do 
  94. it through FUNCTION CALLS. The C language has control structures, arithmetic 
  95. operators, and logical test commands, but no i/o commands - that is the job
  96. of the library. Every C compiler comes with a library, and it's basically 
  97. standardized - for example, printf will almost always be printf, but the 
  98. compiler writer could have just as easily called it print_the_fucking_stuff 
  99. if he really wanted to. 
  100.  
  101.  
  102. A C program is made up of functions and function calls. The *MUST* be one 
  103. function, called main() which is what gets called when you first execute the 
  104. program... Here is an example of a very basic C program:
  105.  
  106. #include <stdio.h>
  107.  
  108. void main(void) {
  109.    clrscr();
  110.    printf("\n\nhElLo cReWeL, EViL, aNd WiCKED wOrLd!%$^!$!!^%$!^%$!!\n\n");
  111. }
  112.  
  113. This illustrates several important things:
  114.  
  115. Line 1 (the #include line): The #include command isn't a C command, it's a 
  116. command to the compiler to include (I bet you would have never figured that 
  117. out!!!) another file into the compilation. In this case, we are including the 
  118. stANdARD iNPUT/oUTPUT library header file. We need to do this because without 
  119. it, the compiler won't know what the fuck we're talking about when we use any 
  120. i/o functions - they're defined in stdio.h ...
  121.  
  122. Line 2: This declares the main() function. The first void tells the compiler 
  123. that main() doesn't return any value to the caller (since there isn't one!), 
  124. while the second void tells the compiler that main() doesn't require any 
  125. parameters. The { opens the function block.
  126.  
  127. Line 3: This is a call to the library's clear screen routine.
  128.  
  129. Line 4: This prints a string. The "\n"'s are NEWLINES, they tell the function 
  130. to go to the next line. You will also notice that all statement lines and in 
  131. a ";"...
  132.  
  133. Line 5 (}): This ends main()'s function block, and also in this case the 
  134. program. 
  135.  
  136. This illustrates several important points, as well as carries on the 
  137. traditional of ALWAYS making programming students write a "hello world" 
  138. program as their first. Of course, I altered the text, because I'm scum, but 
  139. ohwell. 
  140.  
  141.  
  142. Here is another sample program:
  143.  
  144. #include <stdio.h>
  145.  
  146. void main(void) {
  147.    register int i;
  148.    char inpstr[81];
  149.    for(i=0;i<100;i++) {
  150.       clrscr();
  151.       printf("\n\nInput a string: ");
  152.       gets(inpstr);
  153.       printf("\n\nYou typed: %s, number %i\n",inpstr,i);
  154.       gets(inpstr);
  155.    }
  156. }
  157.  
  158. Ok, the first few lines should already be familiar for you. I'm going to 
  159. start with the "for..." line. This illustrates an important thing in C - the 
  160. for loop. Most other languages have this - in BASIC its FOR I = 1 TO 100. The 
  161. C version is a bit harder to understand, yet infinitely more powerful. The 
  162. first part ("i=0;") set the loop counter to 0. The second part ("i<100";) 
  163. specifies what condition to test when deciding if the loop is done yet. In 
  164. this case, as long as i is less than 100, the loop will execute. The last 
  165. part ("i++") specifies the increment command. This command is what gets 
  166. executed to increment the loop counter. In this case, i gets 1 added to it 
  167. every time the loop executes. The "++" operator is VERY important: In other 
  168. languages, you have to code i = i + 1 - in C, the i = i + 1 is valid - 
  169. however, it will be less efficient when compiled. The reason for the is that 
  170. the i=i+1 version compiles to an ADD instruction, while i++ would compile to 
  171. a simple INC instruction. This is far more efficient. This is called "C 
  172. SHORTHAND", and there are several others as well: i-- (subtract 1), i+=3 
  173. (i=i+3), and so on. Of course, the variable doesn't have to be i, could be 
  174. any other variable.
  175.  
  176. Another VERY important concept is demonstrated by this for loop as well - the 
  177. BLOCK STATEMENT. Any place a normal single statement is valid, a block 
  178. statement enclosed by { and } is also valid. In this example, everything 
  179. between the { and } will get executed on each iteration of the loop.
  180.  
  181. clrscr() is the Borland library function to clear the screen - other 
  182. compilers may vary. 
  183.  
  184. The first printf statement should be quite obvious.
  185.  
  186. gets() is one of the Borland library functions to input a string from the 
  187. keyboard. Quite simple.
  188.  
  189. The next printf line shows off part of the true power of printf. The string 
  190. between the quotes in any printf statement is really a FORMAT STRING, and in 
  191. most cases, will just be output as is. However, if you insert any % commands, 
  192. printf will expect additional arguments to tell it what to print in their 
  193. place. The %s means put a string at this position, and %i means use an 
  194. integer. For every % command, you must have a variable, separated by commas 
  195. outside the closing quote, else you will crash your program. In this case, 
  196. printf will print "You typed: " then whatever you typed into inpstr, then ", 
  197. number " and the current value of i. This makes printf very powerful as 
  198. I'm sure you can see. 
  199.  
  200. The last gets() line just makes you press enter to continue.
  201.  
  202.  
  203.  
  204.  
  205. I've covered quite a bit here, and you can actually do quite a bit with this 
  206. small amount of knowledge, if you're creative enough. Next issue, I'll cover 
  207. pointers, bit fields, structures, and basic file accessing. 
  208.  
  209.  
  210.  
  211. Call:
  212.                                                                    
  213. …ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
  214. ∫   DnA Systems, Inc. - DnA World Headquarters & Order Processing Center  ∫
  215. ∫         hack/phreak/anarchy/scams/virii+trojans/anti-gov't ONLY         ∫
  216. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  217. ∫          177 Issues of Computer Underground Digest (Vol. 1-5)           ∫
  218. ∫                           ALL Issues of Phrack                          ∫
  219. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  220. ∫  Legion of Doom  *  P/HUN  *  40 Hex  *  Phalcon Virus Writing Guides   ∫
  221. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  222. ∫                           476 Viruses On-Line                           ∫
  223. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  224. ∫             The Sixth Column Western US Distribution Center             ∫
  225. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  226. ∫    CyberCrime International Network 714 Area Coordinator (69:5714/0)    ∫
  227. ∫                     Platinum Net 714 Hub (93:7714/0)                    ∫
  228. ∫                          BOCNet Node (14:313/7)                         ∫
  229. «ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ∂
  230. ∫     714-646-9180  *  714-646-9180  *  714-646-9180  *  714-646-9180     ∫
  231. ∫     714-646-9180  *  714-646-9180  *  714-646-9180  *  714-646-9180     ∫
  232. »ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº